﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using UniRlv.COL;
using UniRlv.UI.Main.Widget;
using UniRlv.Utility;

namespace UniRlv.UI.Main.WarerHourseIn
{
    public partial class FrmWarehourseInStatus : UniRlv.UI.Main.FrmBase
    {
        private FrmStationStatus dspStation1 = null, dspStation2 = null, dspStation3 = null, dspStation4 = null;
        private FrmStationStatus frm1 = null, frm2 = null, frm3 = null, frm4 = null;
        private FrmInOperate frmOpr = null;
        private FrmCriticalError2 frmErr = null;

        TableLayoutPanel tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();

        public FrmWarehourseInStatus()
        {
            InitializeComponent();

            tableLayoutPanel1.Dock = DockStyle.Fill;
            this.Controls.Add(tableLayoutPanel1);
            tableLayoutPanel1.BringToFront();
        }

        private void FrmWarehourseInStatus_Load(object sender, EventArgs e)
        {
            //toolStrip1.Visible = false;
            toolBtnRun.Visible = true;
            toolBtnRun.Click += toolBtnRun_Click;

            toolBtnStop.Visible = true;
            toolBtnStop.Click += toolBtnStop_Click;

            toolBtnRefresh.Visible = true;
            toolBtnRefresh.Click += toolBtnRefresh_Click;

            WorkSpace.GetInstance().TaskPauseEvent += TaskPauseEventHandler;
            WorkSpace.GetInstance().TaskResumeEvent += TaskResumeEventHandler;
            FrmMain.GetInstance().CodeRefreshEvent += CodeRefreshEventHandler;

            if (PublicObject.CurTask == null)
            {
                return;
            }
            else
            {
                if (PublicObject.CurTask.state == (int)UniRlv.COL.task_state.TS_RUN)
                {
                    toolBtnStop.Enabled = true;
                    toolBtnRun.Enabled = false;
                }
                else
                {
                    toolBtnStop.Enabled = false;
                    toolBtnRun.Enabled = true;
                }
            }
            int levels = PublicObject.CurTask.tagLevel;

            int[] ratios = AppHelper.GetLevelRatios(PublicObject.CurTask.tagRatio);

            frmOpr = new FrmInOperate();
            frmOpr.TopLevel = false;
            frmOpr.Dock = DockStyle.Fill;
            frmOpr.Show();

            frmErr = new FrmCriticalError2();
            frmErr.TopLevel = false;
            frmErr.Dock = DockStyle.Fill;
            frmErr.Show();

            switch (levels)
            {
                case 1:

                    #region 一级关联
                    tableLayoutPanel1.RowCount = 2;
                    tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 90));
                    tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 10));

                    tableLayoutPanel1.ColumnCount = 2;
                    tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 90));
                    tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 10));

                    frm1 = new FrmStationStatus(1, 1, true);
                    frm1.m_nTopRatio = AppHelper.GetStationRatio2(1, PublicObject.CurTask.tagRatio);
                    frm1.TopLevel = false;
                    frm1.Dock = DockStyle.Fill;
                    frm1.Show();

                    tableLayoutPanel1.Controls.Add(frm1, 0, 0);
                    tableLayoutPanel1.Controls.Add(frmOpr, 1, 0);
                    tableLayoutPanel1.Controls.Add(frmErr, 0, 1);

                    tableLayoutPanel1.SetRowSpan(frmOpr, 2);
                    #endregion

                    break;
                case 2:
                    #region 二级关联
             
                    tableLayoutPanel1.RowCount = 2;
                    tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 90));
                    tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 10));

                    tableLayoutPanel1.ColumnCount = 3;
                    tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 45));
                    tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 45));
                    tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 10));

                    frm2 = new FrmStationStatus(2, 1, true);
                    frm2.m_nTopRatio = AppHelper.GetStationRatio2(2, PublicObject.CurTask.tagRatio);
                    frm2.TopLevel = false;
                    frm2.Dock = DockStyle.Fill;
                    frm2.Show();

                    tableLayoutPanel1.Controls.Add(frm2, 1, 0);

                                        
                    frm1 = new FrmStationStatus(1, ratios[0], false);
                    frm1.m_nTopRatio = AppHelper.GetStationRatio2(1, PublicObject.CurTask.tagRatio);
                    frm1.TopLevel = false;
                    frm1.Dock = DockStyle.Fill;
                    frm1.Show();

                    tableLayoutPanel1.Controls.Add(frm1, 0, 0);

                    tableLayoutPanel1.Controls.Add(frmOpr, 2, 0);

                    tableLayoutPanel1.Controls.Add(frmErr, 0, 1);

                    tableLayoutPanel1.SetColumnSpan(frmErr, 2);
                    tableLayoutPanel1.SetRowSpan(frmOpr, 2);
                    #endregion
                    break;
                case 3:

                    #region 三级关联
                    tableLayoutPanel1.RowCount = 2;
                    tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 90));
                    tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 10));

                    tableLayoutPanel1.ColumnCount = 4;
                    tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 30));
                    tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 30));
                    tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 30));
                    tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 10));



                    frm3 = new FrmStationStatus(3, 1, true);
                    frm3.m_nTopRatio = AppHelper.GetStationRatio2(3, PublicObject.CurTask.tagRatio);
                    frm3.TopLevel = false;
                    frm3.Dock = DockStyle.Fill;
                    frm3.Show();
                    tableLayoutPanel1.Controls.Add(frm3, 2, 0);

                    frm2 = new FrmStationStatus(2, ratios[0], false);
                    frm2.m_nTopRatio = AppHelper.GetStationRatio2(2, PublicObject.CurTask.tagRatio);
                    frm2.TopLevel = false;
                    frm2.Dock = DockStyle.Fill;
                    frm2.Show();
                    tableLayoutPanel1.Controls.Add(frm2, 1, 0);

                                       
                    frm1 = new FrmStationStatus(1, ratios[1], false);
                    frm1.m_nTopRatio = AppHelper.GetStationRatio2(1, PublicObject.CurTask.tagRatio);
                    frm1.TopLevel = false;
                    frm1.Dock = DockStyle.Fill;
                    frm1.Show();

                    tableLayoutPanel1.Controls.Add(frm1, 0, 0);

                    tableLayoutPanel1.Controls.Add(frmOpr, 3, 0);
                    tableLayoutPanel1.Controls.Add(frmErr, 0, 1);

                    tableLayoutPanel1.SetColumnSpan(frmErr, 3);
                    tableLayoutPanel1.SetRowSpan(frmOpr, 2);

                    #endregion

                    break;
                case 4:
                    #region 四级关联

                    tableLayoutPanel1.ColumnCount = 3;
                    tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 45));
                    tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 45));
                    tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 10));

                    tableLayoutPanel1.RowCount = 3;
                    tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 45));
                    tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 45));
                    tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 10));

                    frm4 = new FrmStationStatus(4, 1, true);
                    frm4.m_nTopRatio = AppHelper.GetStationRatio2(4, PublicObject.CurTask.tagRatio);
                    frm4.TopLevel = false;
                    frm4.Dock = DockStyle.Fill;
                    frm4.Show();
                    tableLayoutPanel1.Controls.Add(frm4, 1, 1);

                                        
                    frm3 = new FrmStationStatus(3, ratios[0], false);
                    frm3.m_nTopRatio = AppHelper.GetStationRatio2(3, PublicObject.CurTask.tagRatio);
                    frm3.TopLevel = false;
                    frm3.Dock = DockStyle.Fill;
                    frm3.Show();
                    tableLayoutPanel1.Controls.Add(frm3, 0, 1);

                    frm2 = new FrmStationStatus(2, ratios[1], false);
                    frm2.m_nTopRatio = AppHelper.GetStationRatio2(2, PublicObject.CurTask.tagRatio);
                    frm2.TopLevel = false;
                    frm2.Dock = DockStyle.Fill;
                    frm2.Show();
                    tableLayoutPanel1.Controls.Add(frm2, 1, 0);

                    
                    frm1 = new FrmStationStatus(1, ratios[2], false);
                    frm1.m_nTopRatio = AppHelper.GetStationRatio2(1, PublicObject.CurTask.tagRatio);
                    frm1.TopLevel = false;
                    frm1.Dock = DockStyle.Fill;
                    frm1.Show();
                    tableLayoutPanel1.Controls.Add(frm1, 0, 0);

                    tableLayoutPanel1.Controls.Add(frmOpr, 2, 0);
                    tableLayoutPanel1.SetRowSpan(frmOpr, 2);
                    tableLayoutPanel1.Controls.Add(frmErr, 0, 2);

                    tableLayoutPanel1.SetColumnSpan(frmErr, 2);
                    tableLayoutPanel1.SetRowSpan(frmOpr, 3);
                    #endregion


                    break;
                default:
                    break;
            }

            if (Screen.AllScreens.Length > 1 && levels>1)
            {
                dspStation1 = new FrmStationStatus(2, ratios[levels - 2], true);

                showOnMonitor(dspStation1, 1);
            }

            if (Screen.AllScreens.Length > 2 && levels > 2)
            {
                dspStation2 = new FrmStationStatus(3, ratios[levels-3], false);
                showOnMonitor(dspStation2, 2);
            }

            if (Screen.AllScreens.Length > 4 &&  levels>3)
            {
                dspStation3 = new FrmStationStatus(4, ratios[levels - 4], false);
                showOnMonitor(dspStation3, 3);
            }

        }

        private void CodeRefreshEventHandler(object sender, EventArgs e)
        {
            toolBtnRefresh_Click(this, null);
        }

        void toolBtnRefresh_Click(object sender, EventArgs e)
        {

            try
            {
                if (dspStation4 != null)
                {
                    dspStation4.ScanRefresh();
                }

                if (dspStation3 != null)
                {
                    dspStation3.ScanRefresh();
                }

                if (dspStation2 != null)
                {
                    dspStation2.ScanRefresh();
                }

                if (dspStation1 != null)
                {
                    dspStation1.ScanRefresh();
                }


                if (frm4 != null)
                {
                    frm4.ScanRefresh();
                }

                if (frm3 != null)
                {
                    frm3.ScanRefresh();
                }

                if (frm2 != null)
                {
                    frm2.ScanRefresh();
                }

                if (frm1 != null)
                {
                    frm1.ScanRefresh();
                }
            }
            catch (Exception)
            {

            }

        }

        private void TaskResumeEventHandler(object sender, EventArgs e)
        {
            toolBtnRun.Enabled = false;
            toolBtnStop.Enabled = true;
        }

        private void TaskPauseEventHandler(object sender, EventArgs e)
        {
            toolBtnRun.Enabled = true;
            toolBtnStop.Enabled = false;
        }

        void toolBtnStop_Click(object sender, EventArgs e)
        {
            WorkSpace.GetInstance().PauseTask();
        }

        void toolBtnRun_Click(object sender, EventArgs e)
        {
            WorkSpace.GetInstance().ResumeTask();
        }

        private void FrmWarehourseInStatus_Resize(object sender, EventArgs e)
        {
                       
        }

        private void showOnMonitor(FrmStationStatus frm, int showOnMonitor)
        {
            Screen[] sc;
            sc = Screen.AllScreens;
            if (showOnMonitor >= sc.Length)
            {
                showOnMonitor = sc.Length-1;
            }

            frm.StartPosition = FormStartPosition.Manual;
            frm.Location = new Point(sc[showOnMonitor].Bounds.Left, sc[showOnMonitor].Bounds.Top);
            // If you intend the form to be maximized, change it to normal then maximized.  
            frm.WindowState = FormWindowState.Normal;
            frm.WindowState = FormWindowState.Maximized;
        }

        private void FrmWarehourseInStatus_FormClosed(object sender, FormClosedEventArgs e)
        {
            WorkSpace.GetInstance().TaskResumeEvent -= TaskResumeEventHandler;
            WorkSpace.GetInstance().TaskPauseEvent -= TaskPauseEventHandler;

            FrmMain.GetInstance().CodeRefreshEvent -= CodeRefreshEventHandler;
        }

        public override void OnScanCode(string orgCode, string validCode)
        {
            if (frmOpr.FrmCurExp != null)
            {
                frmOpr.FrmCurExp.AddCode(orgCode);
            }
        }

        public override bool IsInit()
        {
            bool bInit = true;

            if (dspStation4 != null)
            {
                bInit &= dspStation4.bInit;
            }

            if (dspStation3 != null)
            {
                bInit &= dspStation3.bInit;
            }

            if (dspStation2 != null)
            {
                bInit &= dspStation2.bInit;
            }

            if (dspStation1 != null)
            {
                bInit &= dspStation1.bInit;
            }


            if (frm4 != null)
            {
                bInit &= frm4.bInit;
            }

            if (frm3 != null)
            {
                bInit &= frm3.bInit;
            }

            if (frm2 != null)
            {
                bInit &= frm2.bInit;
            }

            if (frm1 != null)
            {
                bInit &= frm1.bInit;
            }

            return bInit;
        }
    }
}
